import numpy as np
import cv2

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

img = cv2.imread('1.jpg')

# 沿着横纵轴放大1.6倍，之后平移（-150，-240），最后沿原图大小截取，等效于剪裁并放大
M_crop_dog = np.array([
    [1.6, 0, -150],
    [0, 1.6, -240]
], dtype=np.float32)

# cv2.warpAffine(原始图像, 变换矩阵,变换后的图像大小)
img_dog = cv2.warpAffine(img, M_crop_dog,(400, 400))
# cv2.imshow("img_1", img_dog)


# x轴的剪切变换，逆时针旋转角度15°
theta=15*np.pi/180
M_shear=np.array([
    [1,np.tan(theta),0],
    [0,1,0]
],dtype=np.float32)

img_sheared=cv2.warpAffine(img,M_shear,(400,600))
# cv2.imshow("img_sheared",img_sheared)

# 顺时针旋转，角度15°
M_rotate=np.array([
    [np.cos(theta),-np.sin(theta),0],
    [np.sin(theta),np.cos(theta),0]
],dtype=np.float32)

im_rotate=cv2.warpAffine(img,M_rotate,(400,600))
# cv2.imshow("im_rotate",im_rotate)
# plt.imshow(im_rotate)  #image表示待处理的图像
# plt.show()



# 旋转+缩放+旋转组合，可以通过SVD分解理解
M=np.array([
    [1,1.5,-400],
    [0.5,2,-100]
],dtype=np.float32)

img_transformed=cv2.warpAffine(img,M,(400,600))
# cv2.imshow("img_transformed",img_transformed)
cv2.imwrite('img_transformed.png', img_transformed)

# plt.imshow(img_transformed)  #image表示待处理的图像
# plt.show()



plt.subplot(121);plt.imshow(img_transformed)
plt.subplot(122);plt.imshow(img_sheared)
# cv2.waitKey(0)

plt.show()

